

// var Url = 'http://47.100.2.17:80/'
var Url = 'https://www.xbbsport.com/'
// var Url = 'http://127.0.0.1:80/'

var openId = ''
Page({

  /**
   * 页面的初始数据
   */
  data: {
    src: '../image/main_bg.jpg',
    logo: '../image/he_logo.png',
    showView: false

  },

  getCounopBtn: function (e) {
    console.log(openId)
    if (openId != null && openId != "") {
      wx.navigateTo({
        url: 'index?openId=' + openId
      })
      // wx.redirectTo({
      //   url: 'index?openId=' + openId
      // })
    }

  },
  getScanBtn: function (e) {
    wx.scanCode({
      success: (res) => {
        console.log("扫码结果");
        console.log(res);
        wx.request({
          url: Url + "coupon/use?openId=" + res,
          success: function (res) {
            wx.showModal({
              title: '提示',
              content: '验证结果:' + res.data.Data,
              success: function (res) {
                if (res.confirm) {
                  console.log('用户点击确定')
                }
              }
            })
          }
        })

      },
      fail: (res) => {
        console.log(res);
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    // 页面初始化 options为页面跳转所带来的参数
    // var size = this.setCanvasSize();//动态设置画布大小
    // var initUrl = this.data.placeholder;
    // this.createQrCode(initUrl, "mycanvas", size.w, size.h);

    // wx.getUserInfo({
    //   success: function (res) {
    //     console.log(res)
    //     // that.setData({
    //     //   placeholder: res.userInfo,
    //     // });
    //     // that.createQrCode(that.data.placeholder, "mycanvas", size.w, size.h);
    //   }
    // })
    var that = this;
    wx.login({
      success: function (res) {
        console.log(res)
        if (res.code) {
          wx.showToast({ title: '登录成功提示', })
          that.getOpenId(res);

        } else {
          wx.showToast({ title: '登录失败', })
        }

      },
      complete: function (res) {

      }

    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },


  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {


  },

  getOpenId: function (res) {
    var that = this;
    wx.request({
      url: Url + 'coupon/getOpenId?code=' + res.code,
      header: {
        'content-type': 'json'
      },
      method: 'GET',
      success: function (res) {
        wx.showToast({
          title: '请求openId成功',
        })
        openId = res.data.openid
        that.checkIsAdmin(res)

      },
      fail: function (res) {
        wx.showToast({
          title: '请求openId失败' + res.data,
        })

      },
      //  complete: function (res) {
      //    console.log(res.data)
      //    wx.showToast({
      //      title: "?" + res.data,
      //    })
      //  }

    })
  },
  checkIsAdmin: function (res) {
    var that = this;
    wx.request({
      url: Url + "coupon/isAdmin?openId=" + res.data.openid,
      header: {
        'content-type': 'json'
      },
      method: 'GET',
      success: function (result) {
        wx.showToast({
          title: '验证admin成功',
        })
        console.log(result.data.Data)
        if (result.data.Data == 1) {
          that.setData({
            showView: true
          })

        } else {
          wx.showToast({ title: "正在生成体验券" })
          wx.redirectTo({
            url: 'index?openId=' + res.data.openid
          })
        }
      }
    })
  }


})